package com.syp.auth.dao.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.syp.auth.dao.po.UserRoleEntity;

@Repository
public interface UserRoleRepository extends JpaRepository<UserRoleEntity, Long>, JpaSpecificationExecutor<UserRoleEntity> {

	/**
	 * 根据用户Id删除
	 * 
	 * @param userId
	 * @return
	 */
	@Query("DELETE UserRoleEntity WHERE userId=:userId")
	@Modifying
	public int deleteByUserId(@Param("userId") long userId);

	/**
	 * 根据用户Id删除
	 * 
	 * @param userId
	 * @return
	 */
	@Query("DELETE UserRoleEntity WHERE userId=:userId AND roleId in(:roleIdList)")
	@Modifying
	public int deleteByUserIdLimitedInMyDomain(@Param("userId") long userId, @Param("roleIdList") List<Long> roleIdList);

	/**
	 * 根据用户Id删除
	 * 
	 * @param userId
	 * @return
	 */
	@Query("DELETE UserRoleEntity WHERE userId in(:userIdList)")
	@Modifying
	public int deleteByUserIdList(@Param("userIdList") List<Long> userIdList);

	/**
	 * 根据角色id删除
	 * 
	 * @param roleId
	 * @return
	 */
	@Query("DELETE UserRoleEntity WHERE roleId=:roleId")
	@Modifying
	public int deleteByRoleId(@Param("roleId") long roleId);

	/**
	 * 根据用户Id删除
	 * 
	 * @param userId
	 * @return
	 */
	@Query("DELETE UserRoleEntity WHERE roleId in(:roleIdList)")
	@Modifying
	public int deleteByRoleIdList(@Param("roleIdList") List<Long> roleIdList);

	/**
	 * 根据用户id查询
	 * 
	 * @param userId
	 * @return
	 */
	@Query("FROM UserRoleEntity WHERE userId=:userId")
	@Modifying
	public List<UserRoleEntity> getByUserId(@Param("userId") long userId);
}